package com.tbs.parkingsaleapp.DataTool;

import com.tbs.parkingsaleapp.Http.JsonData.DLocation;
import com.tbs.parkingsaleapp.Http.JsonData.Houseinfo;
import com.tbs.parkingsaleapp.Http.JsonData.LoginResult;
import com.tbs.parkingsaleapp.Http.JsonData.ParkResult;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;

public class RecommendationCalculator {

    private static final double EARTH_RADIUS = 6378.137;//地球半径,单位千米
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }
    private static double getDistance(double lat1,double lng1,double lat2,double lng2)
    {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;

    }
    public static double distance(DLocation a,DLocation b)
    {
        return getDistance(a.getLatitude(),a.getLongitude(),b.getLatitude(),b.getLongitude());
    }
    public static double fromUser(DLocation location)
    {
        if(SystemDataMananger.isLogined())
        {
            return discheck(SystemDataMananger.getLogined().getHouseinfo(),location);
        }
        else
            return -1;
    }
    private static double discheck(Houseinfo[] houseinfos, DLocation tar)
    {
        double p=Double.MAX_VALUE;
        for (Houseinfo h:houseinfos
        ) {
            p=Math.min(distance(h.getLocation(),tar),p);
        }
        return p;
    }
    //park = 需要计算推荐度的车位信息 user=当前已登录的用户
    public static double[] value(ParkResult park, LoginResult user)
    {

        if(!SystemDataMananger.Buyable(park))//对该车位是否有购买权
            return new double[]{0,0,0,0};
        double dis= RecommendationCalculator.fromUser(SystemDataMananger.FindLocation(park.getPark().getLocation()).getLocation())+0.1;//距离用户所拥有的房产的最短距离
        boolean[] parks= park.getTrait().raw();boolean[] users=user.getLikeInfo().raw();
        double price=park.getPark().getPrice();
        int cnt=park.getOrderedCount();
        double[] v= Cal.value(cnt,price,dis,parks,users);
        return v;
    }

}
